package vip.meet;

import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Slf4j
public class Start {

    public static void main(String[] args) throws SQLException, InterruptedException {
        System.out.println("ok");
        Connection connection = DBConnectionFactory.getConnection("base");
        try {
            doSomething(connection);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            connection.rollback();
        }
        System.out.println("done");
        TimeUnit.MINUTES.sleep(1);
        System.out.println("final");

    }

    private static void doSomething(Connection connection) throws SQLException, InterruptedException {

        connection.setAutoCommit(false);
        // 创建Statement对象执行SQL语句
        Statement statement = connection.createStatement();

        // 设置回滚点
        Savepoint savepoint = connection.setSavepoint("MySavepoint");
        // 执行SQL语句
        statement.executeUpdate("INSERT INTO user_test (name) VALUES ('cc')");
        statement.executeUpdate("INSERT INTO user_test (name) VALUES ('dd')");

        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(() -> {
            Connection c = null;
//            try {
//                c = DBConnectionFactory.getConnection("base");
//            } catch (SQLException e) {
//                log.error(e.getMessage(), e);
//            }
//            if (c != null) {
//                try {
//                    c.setAutoCommit(false);
//                } catch (SQLException e) {
//                    log.error(e.getMessage(), e);
//                }
            // 创建Statement对象执行SQL语句
//                Statement s = null;
//                try {
//                    s = c.createStatement();
//                } catch (SQLException e) {
//                    log.error(e.getMessage(), e);
//                }
            for (int i = 0; i < 5; i++) {
                try {
//                        assert s != null;
                    statement.executeUpdate("INSERT INTO user_test (name) VALUES ('f-" + i + "')");
                    System.out.println("执行成功：" + i);
                    TimeUnit.SECONDS.sleep(5);
                } catch (SQLException | InterruptedException e) {
                    log.error("执行出错：{}{}", i, e.getMessage(), e);
                }
            }
            try {
//                    c.commit();
                connection.commit();
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
//            }
        });

        TimeUnit.SECONDS.sleep(10);
        System.out.println(1 / 0);
        // 提交事务
        connection.commit();
        System.out.println("事务成功提交！");
    }

    private static class AddData implements Runnable {

        @Override
        public void run() {
            Connection c = null;
            try {
                c = DBConnectionFactory.getConnection("base");
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
            if (c != null) {
//                try {
//                    c.setAutoCommit(false);
//                } catch (SQLException e) {
//                    log.error(e.getMessage(), e);
//                }
                // 创建Statement对象执行SQL语句
                Statement s = null;
                try {
                    s = c.createStatement();
                } catch (SQLException e) {
                    log.error(e.getMessage(), e);
                }
                for (int i = 0; i < 5; i++) {
                    try {
                        assert s != null;
                        s.executeUpdate("INSERT INTO user (name) VALUES ('" + i + "')");
                        System.out.println("执行成功：" + i);
                        TimeUnit.SECONDS.sleep(5);
                    } catch (SQLException | InterruptedException e) {
                        log.error("执行出错：{}{}", i, e.getMessage(), e);
                    }
                }
//                try {
//                    c.commit();
//                } catch (SQLException e) {
//                    log.error(e.getMessage(), e);
//                }
            }
        }
    }


}
